package com.shunbo.convenient.utils;

import java.util.concurrent.atomic.AtomicInteger;


public class LongUUidMananger {
	
	private static LongUUidMananger INSTANCE = new LongUUidMananger();
	private LongUidGenerator[] Generators;
	private Thread timeUpdator;
	
	private static final int UID_A = 0;//t_auth 主键ID
	private static final int UID_AM = 1;//t_authtmenu 主键ID，
	private static final int UID_M = 2;//t_menu 主键ID
	private static final int UID_R = 3;//t_role 主键ID
	private static final int UID_RA = 4;//t_roletauth 主键ID
	private static final int UID_U = 5;//t_user 主键ID
	private static final int UID_UR = 6;//t_usertrole 主键ID
	private static final int UID_TOKEN = 7;//t_usertrole 主键ID
	
	private static final int ACTIVITI_ID = 8;//activiti 工作流主键生成策略
	
	private static final int INVESTID_TYPE = 3;//标的主键
	private static final int BILLS_TYPE = 4;//借款账单 
	private LongUUidMananger() {
		Generators = new LongUidGenerator[9];
		for(int i=0;i<Generators.length;i++) {
			Generators[i] = new LongUidGenerator(LongUidGenerator.convertInt("longuuid.instanceid", 0));
		}
		timeUpdator = new Thread("LongUUidMananger-TimeUpdator") {
			public void run() {
				long now = 0;
				while(true) {
					now = System.currentTimeMillis();
					for(int i=0;i<Generators.length;i++) {
						Generators[i].resetTimeHeader(now);
					}
					try {
						Thread.sleep(1010);
					}
					catch(Throwable ignore) {}
				}
			}
		};
		timeUpdator.setDaemon(true);
		timeUpdator.start();
	}
	
	public static long newAuthId() {
		return INSTANCE.Generators[UID_A].genId();
	}
	
	
	public static long newAuthMenuId() {
		return INSTANCE.Generators[UID_AM].genId();
	}
	
	public static long newMenuId() {
		return INSTANCE.Generators[UID_M].genId();
	}
	
	public static long newRoleId() {
		return INSTANCE.Generators[UID_R].genId();
	} 
	public static long newRoleAuthId() {
		return INSTANCE.Generators[UID_RA].genId();
	} 
	
	public static long newUserId() {
		return INSTANCE.Generators[UID_U].genId();
	}
	public static long newChannelId() {
		return INSTANCE.Generators[UID_U].genId();
	}

	public static long newUserRoleId() {
		return INSTANCE.Generators[UID_UR].genId();
	}
	
	public static long newTokenId() {
		return INSTANCE.Generators[UID_TOKEN].genId();
	} 
	
	public static long newActivitiId() {
		return INSTANCE.Generators[ACTIVITI_ID].genId();
	} 
	public static long newInvestId() {
		return INSTANCE.Generators[INVESTID_TYPE].genId();
	} 
	public static long newBillsId() {
		long i=INSTANCE.Generators[BILLS_TYPE].genId();
		return i;
	}
	private static class LongUidGenerator {
		private AtomicInteger seq;
		private volatile long timeHeader;
		private final int instanceId;
		
		public LongUidGenerator(int instanceId) {
			this.seq = new AtomicInteger(0);
			this.timeHeader = System.currentTimeMillis();
			this.instanceId = instanceId;
		}
		
		public void resetTimeHeader(long now) {
			if(now/1000 != this.timeHeader/1000) {
				timeHeader = now;
				seq.set(0);
			}
			else {
			}
		}
		/**
		 * 每秒26w id生成器
		 * @return
		 */
		public long genId() {
			return ((this.timeHeader/1000)<<22)+(instanceId<<18)+seq.getAndIncrement();
		}
		
		@SuppressWarnings("unused")
		public static long time(long id) {
			return (id>>22)*1000;
		}
		public static int convertInt(String str, int def) {
			try {
				return str == null?def:Integer.valueOf(str).intValue();
			} catch (NumberFormatException var3) {
				return def;
			}
		}
	}
	public static void main(String args[]){
		long id = newBillsId();
		System.out.print(id);
	}
}
